package gui;

import java.io.File;

import logic.Player;

import gamemngr.GameMngr;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import resources.Configuration;

import com.swtdesigner.SWTResourceManager;

public class LoadDialog extends Dialog {

	private Combo selectGameCombo;
	protected Object result;
	protected Shell shell;
	private GameMngr game;
	private Color[] playerColours;
	private RiskGUI riskGUI;
	private SaveData saveData;

	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public LoadDialog(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * @param parent
	 * @param saveData 
	 */
	public LoadDialog(Shell parent, GameMngr game, Color[] playerColours, RiskGUI gui, SaveData saveData) {
		this(parent, SWT.NONE);
		this.game = game;
		this.playerColours = playerColours;
		this.riskGUI = gui;
		this.saveData = saveData;
	}

	/**
	 * Open the dialog
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return game;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		shell.setLocation(RiskGUI.getNewPosition(getParent()));
		shell.setSize(208, 136);
		shell.setText("Load");

		final Label chooseTheGameLabel = new Label(shell, SWT.NONE);
		chooseTheGameLabel.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		chooseTheGameLabel.setFont(SWTResourceManager.getFont("Arial", 12, SWT.NONE));
		chooseTheGameLabel.setText("Choose the game to load:");
		chooseTheGameLabel.setBounds(10, 10, 191, 30);

		selectGameCombo = new Combo(shell, SWT.NONE);
		selectGameCombo.setText("Select game");
		selectGameCombo.setBounds(10, 46, 178, 21);

		loadSavedGamesName();

		final Button loadButton = new Button(shell, SWT.NONE);
		loadButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if(selectGameCombo.getText().equalsIgnoreCase("Select game")) return;
				riskGUI.loadGameMngr();
				String fileName = Configuration.saveDir + File.separator + selectGameCombo.getText()+ ".data";
				game = game.loadGame(new File(fileName));
				if(game == null){
					AutoDialogMessegaes.displayErrorMsg("Load", "The game \"" + selectGameCombo.getText() + "\" can't be loaded. The file is corrupted.", shell);
					shell.dispose();
				} else {

					saveData.setSaveName(fileName);
					saveData.setSaved(true);
					checkPlayersColors();
					AutoDialogMessegaes.displayFeedbackMsg("Load", "The game \"" + selectGameCombo.getText() + "\" has been loaded ", shell);
					shell.dispose();
				}
			}
		});
		loadButton.setText("Load");
		loadButton.setBounds(53, 73, 65, 23);

		final Button cancelButton = new Button(shell, SWT.NONE);
		cancelButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				shell.dispose();
			}
		});
		cancelButton.setText("Cancel");
		cancelButton.setBounds(124, 73, 65, 23);
		//
	}

	private void loadSavedGamesName() {
		for(String str : game.getSavedGames()){
			this.selectGameCombo.add(str);
		}
	}

	private void checkPlayersColors(){
		reSetColors();
	}

	private void reSetColors() {
		for(int i=0; i<game.getPlayers().size(); i++){
			game.getPlayers().get(i).setColour(this.playerColours[i]);
		}
	}

}
